package com.example.androidstart.util

import android.os.Build
import android.text.TextUtils
import android.util.Log
import androidx.annotation.RequiresApi
import org.apache.commons.io.FileUtils
import java.io.BufferedReader
import java.io.File
import java.io.FileReader
import java.io.IOException
import java.nio.charset.Charset
import java.nio.file.Files
import java.nio.file.Paths
import java.util.function.Consumer

object FileUtil {
    private const val TAG = "FileUtil"

    @kotlin.jvm.JvmStatic
    fun ReadFileByBufferReaderToString(path: String?): String {
        if (TextUtils.isEmpty(path)) {
            return ""
        }
        val stringBuilder = StringBuilder()
        try {
            BufferedReader(FileReader(path)).use { bufferedReader ->
                var tempStr: String?
                while ((bufferedReader.readLine().also { tempStr = it }) != null) {
                    stringBuilder.append(tempStr).append(System.lineSeparator())
                }
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
        Log.i(TAG, "ReadFileToString: read success ")
        return stringBuilder.toString()
    }

    @kotlin.jvm.JvmStatic
    fun ReadFileByBufferReaderToStringUseString(path: String?): String {
        if (TextUtils.isEmpty(path)) {
            return ""
        }
        var result = ""
        try {
            BufferedReader(FileReader(path)).use { bufferedReader ->
                var tempStr: String
                while ((bufferedReader.readLine().also { tempStr = it }) != null) {
                    result += tempStr
                }
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
        Log.i(TAG, "ReadFileToString: read success ")
        return result
    }

    @kotlin.jvm.JvmStatic
    @RequiresApi(api = Build.VERSION_CODES.O)
    fun ReadFileByReadAllBytesReaderToString(path: String?): String? {
        if (TextUtils.isEmpty(path)) {
            return ""
        }
        var result: String? = null
        try {
            result = String(Files.readAllBytes(Paths.get(path)))
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return result
    }

    @kotlin.jvm.JvmStatic
    @RequiresApi(api = Build.VERSION_CODES.O)
    fun ReadFileByByFilesReadLinesToString(path: String?): String {
        if (TextUtils.isEmpty(path)) {
            return ""
        }
        val stringBuilder = StringBuilder()
        try {
            Files.lines(Paths.get(path)).use { stream ->
                stream.forEach(
                    Consumer<String?> { s -> stringBuilder.append(s) })
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return stringBuilder.toString()
    }

    @kotlin.jvm.JvmStatic
    fun ReadFileByCommonIOReadFileToString(path: String): String {
        if (TextUtils.isEmpty(path)) {
            return ""
        }
        try {
            return FileUtils.readFileToString(File(path), Charset.defaultCharset())
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return ""
    }
}
